package com.atguigu.com.nio.nio_2_channel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author: robert.song
 * @Date: Created in 2021/8/6
 */
public class NIOFileChannel02 {
    public static void main(String[] args) throws IOException {
//        创建文件的输入流
        File file = new File("D:\\file\\project\\cx\\test.log");
        FileInputStream fileInputStream = new FileInputStream(file);
//        通过fileInputStream获取对应的FileChannel-> 即FileChannelImpl实现类
        FileChannel fileChannel = fileInputStream.getChannel();
//        创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
//        将通道的数据读入到buffer
        fileChannel.read(byteBuffer);
        System.out.println(new String(byteBuffer.array()));
        fileInputStream.close();

    }
}
